package com.laiketui.common.mapper;

import com.laiketui.domain.product.BargainGoodsModel;
import com.laiketui.core.exception.LaiKeAPIException;
import com.laiketui.core.db.BaseMapper;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author Trick
 */
public interface BargainGoodsModelMapper extends BaseMapper<BargainGoodsModel> {

    @Override
    List<Map<String, Object>> selectDynamic(Map<String, Object> map) throws LaiKeAPIException;

    @Override
    int countDynamic(Map<String, Object> map) throws LaiKeAPIException;

    /**
     * 获取正在砍价商品信息
     *
     * @param storeId   -
     * @param beginDate -
     * @param endDate   -
     * @param start     -
     * @param end       -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/2/22 16:38
     */
    @Select("SELECT b.id AS bargain_id, b.*, c.* FROM lkt_bargain_goods b LEFT JOIN lkt_product_list a ON b.goods_id = a.id " +
            "LEFT JOIN lkt_configure c ON b.attr_id = c.id LEFT JOIN lkt_mch m ON m.id = a.mch_id " +
            "WHERE b.store_id = #{storeId} AND c.num > 0 AND b.is_delete = 0 AND (b.status = 1 OR b.status = 2) AND b.begin_time < #{beginDate} " +
            "AND is_show = 1 AND b.end_time > #{endDate} AND a.status = 2 AND m.is_open = 1 AND a.recycle = 0 " +
            "ORDER BY b.status ASC, b.sort DESC LIMIT #{start}, #{end}")
    List<Map<String, Object>> selectBargainGoodsInfo(int storeId, Date beginDate, Date endDate, int start, int end) throws LaiKeAPIException;

    @Select("select l.*,c.* from lkt_bargain_goods as l right join lkt_configure as c on l.attr_id=c.id where l.goods_id=#{goodsId} and store_id = #{storeId}")
    List<Map<String, Object>> selectBargainGoodsConfigure(int storeId, int goodsId) throws LaiKeAPIException;

    /**
     * 结束已经到期的活动
     *
     * @param sysDate -
     * @return int
     * @author Trick
     * @date 2021/4/16 15:25
     */
    @Update("update lkt_bargain_goods set status=2 where end_time<=#{sysDate} and is_delete=0")
    int clearBargainByEnd(Date sysDate);

    /**
     * 获取最大序号
     *
     * @param storeId -
     * @return int
     * @author Trick
     * @date 2021/5/13 15:51
     */
    @Update("select MAX(sort) from lkt_bargain_goods where store_id = #{storeId}")
    int getMaxSort(int storeId);

    /**
     * 获取活动商品列表
     *
     * @param map -
     * @return List
     * @throws LaiKeAPIException -
     * @author Trick
     * @date 2021/5/13 15:19
     */
    List<Map<String, Object>> selectBargainGoodsList(Map<String, Object> map) throws LaiKeAPIException;

    int countBargainGoodsList(Map<String, Object> map) throws LaiKeAPIException;

}